<?php

namespace app\api\controller\Goods;

use app\api\model\SpeciModel;
use think\Model;
use think\Request;

trait SpeciGroup
{
    /**
     * @param Request $request
     * @param Model $model
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function list(Request $request)
    {
        $where = [];
        if ($request->has('name')) {
            $where[] = ['name', 'LIKE', '%' . $request->param('name') . '%'];
        }
        if ($request->has('id')) {
            $where[] = ['pid', '=', $request->param('id')];
        }
        return $this->returnJson(
            200,
            'ok',
            $this->model->index($where, $request->param('page', 1), $request->param('size', 10))
        );
    }

    public function getGroupSpec(Request $request)
    {
        $level =$request->param('level');
        return $this->returnJson(200,'ok',$this->model->getGroupSpec($level));
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function add(Request $request)
    {
        $data = $request->only($this->field);
        return $this->returnJson(200, 'ok', $this->model->add($data));
    }

    /**
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id)
    {
        return $this->returnJson(200, 'ok', $this->model->info($id));
    }

    /**
     * @param $id
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit($id, Request $request)
    {
        $data = $request->only($this->field);
        return $this->returnJson(200, 'ok', $this->model->edit($id, $data));
    }

    /**
     * @param int $id 属性id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id)
    {
        return $this->model->del($id) ? $this->returnJson(200, 'ok') : $this->returnJson(500, '属性已绑定商品,无法删除');
    }
    /** 
     * @var array
     */

    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function groupList(Request $request)
    {
        return $this->returnJson(
            200,
            'ok',
            $this->model->indexGroup(
                $request->has('name') ? ['gname' => $request->param('name')] : [],
                $request->param('page', 1),
                $request->param('size', 10)
            )
        );
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function addGroup(Request $request)
    {
        return $this->returnJson(200, 'ok', $this->model->addGroup(['gname' => $request->param('name', '')]));
    }

    /**
     * @param $id
     * @param SpeciModel $model
     * @return \think\response\Json
     */
    public function getGroup($id)
    {
        return $this->returnJson(200, 'ok', $this->model->getGroup($id));
    }

    /**
     * @param $id
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editGroup($id, Request $request)
    {
        return $this->returnJson(200, 'ok', $this->model->editGroup($id, ['gname' => $request->param('name', '')]));
    }

    /**
     * @param $id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delGroup($id)
    {
        return $this->model->deleteGroup($id) ? $this->returnJson(200, 'ok') : $this->returnJson(500, '属性已绑定商品,无法删除');
    }
}
